#ifndef MAPTILE_H
#define MAPTILE_H

#include "../common/common.h"
#include "texture.h"
#include <fstream>

typedef struct SResource 
{
	int resource;
	int amount;
} ResourceType;

typedef std::vector<int> MapTileList;
typedef std::map<int, ResourceType> ResourceMap;
class CMapLoader;
class CMapTileRender;

class CMapTile
{
    friend class CMapLoader;
public:
    CMapTile(int id);
    ~CMapTile();
	void Initialize( );
    void SetTextureData( const char *pdata, int srcwidth, int srcheight, RECT &rect );
    void Render();
	void RenderName();
    CTexture &GetTexture();
    void SaveData(std::ofstream &f);
    bool LoadData(std::ifstream &f);
    void ComputeScanline();
    int GetID();
    const IRect &GetRegion()
    {
        return m_region;
    }
    void SetColor( int color )
    {
        m_color = color;
    }
	void SetEntityPoint( IPoint &point );
	IPoint &GetEntityPoint( );

    void AddAdjacency( int id );
    bool IsAdjacency( int id );

    void OnClicked( );

    void SetName( const TCHAR *name );
	const TCHAR *GetName( )
	{
		return m_name.c_str();
	}

	ResourceMap const &GetResources( )
	{
		return m_resources;
	}
	bool HaveResource( ResourceType& resource )
	{
		ResourceMap::iterator iter = m_resources.find( resource.resource );
		if( iter == m_resources.end() )
			return false;
		return true;
	}
	void AddResources( ResourceType& resource )
	{
		m_resources[resource.resource] = resource;
	}
    
    void SetTextureName( const TCHAR *name );
	void Serialize( CSerialize &s );
    void SetRender( CMapTileRender *pRender );
private:
    int m_id;
    MapTileList m_adjacency;
    CTexture m_texture;
    tstring m_textureName;
    IRect m_region;
	IPoint m_entityPoint; 
	IPoint m_namePoint;
    int m_color;   
    tstring m_name;
	ResourceMap m_resources;
    CMapTileRender *m_pRender;
};

#endif